﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityFramework.Extensions;

namespace DAO
{
    public class RoomMemberDAO
    {
        WebChatDBEntities db = new WebChatDBEntities();

        public void AddRoomMember(RoomMember member)
        {
            db.RoomMembers.Add(member);
            db.SaveChanges();
        }

        public IEnumerable<ChatRoom> GetChatRooms(int userId)
        {
            List<RoomMember> members = db.RoomMembers.Where(m => m.UserId == userId).ToList();

            return members.Select(member => db.ChatRooms.Find(member.RoomId)).ToList();
        }

        public bool DeleteRoomMember(int userId)
        {
            RoomMember member = db.RoomMembers.SingleOrDefault(m => m.UserId == userId);
            if(member == null)
                return false;

            db.RoomMembers.Remove(member);
            db.SaveChanges();
            return true;
        }

        public void DeleteAllMembersOfOneRoom(int roomId)
        {
            db.RoomMembers.Delete(m => m.RoomId == roomId);
            db.SaveChanges();
        }

        public void DeleteOneMemberOfAllRooms(int userId)
        {
            db.RoomMembers.Delete(m => m.UserId == userId);
            db.SaveChanges();
        }
    }
}
